package com.dahong.cloud.client.controller;

import com.dahong.cloud.client.feign.AuthorizeHmacFeignClient;
import com.dahong.cloud.client.feign.TestServerFeignClient;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "TestClientController")
@RestController
@RequestMapping("/client")
public class TestClientController {

    private TestServerFeignClient testServerFeignClient;
    private AuthorizeHmacFeignClient testServerHmacFeignClient;

    @Autowired
    public void setTestServerFeignClient(TestServerFeignClient testServerFeignClient) {
        this.testServerFeignClient = testServerFeignClient;
    }

    @Autowired
    public void setTestTestServerHmacFeignClient(AuthorizeHmacFeignClient testServerHmacFeignClient) {
        this.testServerHmacFeignClient = testServerHmacFeignClient;
    }

    @Operation(description = "正常请求")
    @GetMapping("/test/hi")
    public String getHi() {
        return testServerFeignClient.hi();
    }

    /**
     * 模拟异步请求
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @Operation(description = "模拟异步请求(hmac)")
    @GetMapping("/user/hi")
    public String getAsyncHi() throws ExecutionException, InterruptedException {
        try (ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor(); ) {
            Future<String> submit = executorService.submit(() -> testServerHmacFeignClient.hi());
            return submit.get();
        }
    }
}
